package com.hnust.controller;

import com.hnust.domain.ChoiceTestQuestions;
import com.hnust.domain.Questions;
import com.hnust.domain.TestMessage;
import com.hnust.domain.TestQuestions;
import com.hnust.service.PaperCorrectService;
import com.hnust.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping("/api/paperCorrect")
public class PaperCorrectController {
    @Autowired
    PaperCorrectService paperCorrectService;

    //通过paperId获得考试信息
    @GetMapping("/getTest")//               /api/paperCorrect/getTest
    public Result<TestMessage> getTest(@RequestParam( name = "paperId", required = false) String paperId){
        try{
            System.out.println("paperId:"+paperId);
            List<TestMessage> testMessage = paperCorrectService.getTest(paperId);
            TestMessage message = testMessage.get(0);
            System.out.println(message);
            return new Result<TestMessage>(1,"成功",message);
        }catch (Exception e){
            return new Result<TestMessage>(0,e.getMessage(),null);
        }
    }

    //获取选择题题目
    @GetMapping("/getChoiceQuestions")
    public Result<List<ChoiceTestQuestions>> getChoiceQuestions(@RequestParam("paperId") String paperId){
//        System.out.println("yftyv");
        try{
            System.out.println(paperId);
            //解析
            List<Questions> choiceList = paperCorrectService.getChoiceQuestions(paperId);
            System.out.println(choiceList);
            List<ChoiceTestQuestions> list = new ArrayList<>();
            for(Questions obj:choiceList){
                Integer obj1 = (Integer) obj.getLevel();
                list.add(new ChoiceTestQuestions(obj.getContent(),null,obj1.toString(),null,null,null,null));
            }
            return new Result<List<ChoiceTestQuestions>>(1,"成功",list);
        }catch (Exception e){
            return new Result<List<ChoiceTestQuestions>>(0,e.getMessage(),null);
        }
    }

    //获取判断题
    @GetMapping("/getJudgeQuestions")
    public Result<List<TestQuestions>> getJudgeQuestions(@RequestParam("paperId") String paperId){
        try{
            System.out.println(paperId);
            List<Questions> judgeList = paperCorrectService.getJudgeQuestions(paperId);
            System.out.println(judgeList);
            List<TestQuestions> list = new ArrayList<>();
            for(Questions obj:judgeList){
                Integer obj1 = (Integer) obj.getLevel();
                list.add(new TestQuestions(obj.getContent(),null,obj1.toString()));
            }
            return new Result<List<TestQuestions>>(1,"成功",list);
        }catch (Exception e){
            return new Result<List<TestQuestions>>(0,e.getMessage(),null);
        }
    }

    //获取填空题
    @GetMapping("/getGapQuestions")
    public Result<List<TestQuestions>> getGapQuestions(@RequestParam("paperId") String paperId){
        try{
            System.out.println("paperId="+paperId);
            List<Questions> gapList = paperCorrectService.getGapQuestions(paperId);
            System.out.println(gapList);
            List<TestQuestions> list = new ArrayList<>();
            for(Questions obj:gapList){
                Integer obj1 = (Integer) obj.getLevel();
                list.add(new TestQuestions(obj.getContent(),null,obj1.toString()));
            }
            return new Result<List<TestQuestions>>(1,"成功",list);
        }catch (Exception e){
            return new Result<List<TestQuestions>>(0,e.getMessage(),null);
        }
    }

    //获取简答题
    @GetMapping("/getEssayQuestions")
    public Result<List<TestQuestions>> getEssayQuestions(@RequestParam("paperId") String paperId){
        try{
            System.out.println(paperId);
            List<Questions> essayList = paperCorrectService.getEssayQuestions(paperId);
            System.out.println(essayList);
            List<TestQuestions> list = new ArrayList<>();
            for(Questions obj:essayList){
                Integer obj1 = (Integer) obj.getLevel();
                list.add(new TestQuestions(obj.getContent(),null,obj1.toString()));
            }
            return new Result<List<TestQuestions>>(1,"成功",list);
        }catch (Exception e){
            return new Result<List<TestQuestions>>(0,e.getMessage(),null);
        }
    }
}
